package com.grep.base.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 
 * @author yuyue z_yuguangyue@boco.com.cn
 * Description:将汉字转化为对应的拼音
 * CreateTime: 2012.6.13
 * version 2.0 <br> 
 * 
 */
public class PinYin4jUtil {
	/**
	 * @author yuyang
	 * @param src ：传入要转化为拼音的汉字
	 * @return:返回所传入汉字转化的拼音
	 */
	public static String getPinyin(String src) {
		char[] arraychar = null;
		arraychar = src.toCharArray();
		String[] arraystring = new String[arraychar.length];
		HanyuPinyinOutputFormat hpof = new HanyuPinyinOutputFormat();
		hpof.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		hpof.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		hpof.setVCharType(HanyuPinyinVCharType.WITH_V);
		String pinyin = "";
		int t0 = arraychar.length;
		try {
			for (int i = 0; i < t0; i++) {
				if (Character.toString(arraychar[i]).matches("[\\u4E00-\\u9FA5]+")) {
					arraystring = PinyinHelper.toHanyuPinyinStringArray(
							arraychar[i], hpof);
					pinyin += arraystring[0];
				} else {
					pinyin += Character.toString(arraychar[i]);
				}
			}
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			e.printStackTrace();
		}
		return pinyin;
	}
	//	/**
	//	 * 此方法存在bug,不能准确输出汉字拼音如：女生
	//	 */
	// public static String getPinyin(String chineseWord){
	// String pinyin = "";
	// HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
	// outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	// try {
	// pinyin = PinyinHelper.toHanyuPinyinString(chineseWord, outputFormat, "");
	// } catch (BadHanyuPinyinOutputFormatCombination e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return pinyin;
	// }
	// public static String getPinyin(String chineseWord,String separate){
	//
	// String pinyin = "";
	// HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
	// outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	// try {
	// pinyin = PinyinHelper.toHanyuPinyinString(chineseWord, outputFormat,
	// separate);
	// } catch (BadHanyuPinyinOutputFormatCombination e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// return pinyin;
	// }
	//

}
